package top.wardyan.wbaseswt.control;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import top.wardyan.wbaseswt.conf.Const;
import top.wardyan.wbaseswt.util.KeyCode;
import top.wardyan.wbaseswt.util.Msg;


/**
 * SWT
 * 2020-02-27 20:21:15
 * @author ward
 * @version 1 
 */
public class WText extends Text{
	
	protected WText self;
	
	public WText(Composite parent, int style) {
		this(parent, style, null, null, 0);
	}
	
	public WText(Composite parent, int style, String text, String tip) {
		this(parent, style, text, tip, 0);
	}
	
	public WText(Composite parent, int style, String text, String tip, int limit) {
		super(parent, style);
		if(text != null) {
			this.setText(text);
		}
		if(tip != null) {
			this.setToolTipText(tip);
		}
		if(limit > 0) {
			this.setTextLimit(limit);
		}
		this.createContent();
	}
	
	private void createContent() {
		self = this;
		this.setFont(Const.getDefaultFont());
		this.setBackground(Const.COLOR_WHITE);
		this.setForeground(Const.COLOR_BLACK);
		
		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.keyCode == KeyCode.KEY_ESC && Msg.confirm(self.getShell(), "警告", "确认关闭吗? 未保存的数据会丢失而且无法恢复", SWT.CANCEL | SWT.OK) == SWT.OK) {
					self.getShell().dispose();
				}
					
			}
		});
		/**
		if(!this.getEditable()) {
			this.setToolTipText((this.getToolTipText() != null ? this.getToolTipText() : "") + "\n\r只读");
		}
		**/
	}
	
	
	@Override
	protected void checkSubclass() {}
	
	/**
	 * 取得控件右下角的定点的x
	 * @return
	 */
	public int getVertexX() {
		return this.getLocation().x + this.getSize().x;
	}
	/**
	 * 取得控件右下角的定点的y
	 * @return
	 */
	public int getVertexY() {
		return this.getLocation().y + this.getSize().y;
	}

	@Override
	public void setEditable(boolean editable) {
		super.setEditable(editable);
		if(!editable) {
			this.setBackground(Const.COLOR_GRAY);
			//this.setToolTipText((this.getToolTipText() != null && this.getToolTipText().length() > 0 && !this.getToolTipText().endsWith("只读") ? this.getToolTipText() + "\n\r" : "") + "只读");
		}else {
			this.setBackground(Const.COLOR_WHITE);
			//this.setToolTipText((this.getToolTipText() != null && this.getToolTipText().length() > 0 && !this.getToolTipText().endsWith("只读") ? this.getToolTipText().substring(0, this.getToolTipText().length() - 2) : this.getToolTipText()));
		}
	}
 
	//protected <T extends BaseModel<T>> T model;
	/**
	protected T model;
	protected String field;
	public void bind(T model, String field) {
		this.model = model;
		this.field = field;
	}
	
	public void show() {
		if(model != null) {
			show(model.getStr(field));
		}
		
	}
	public void show(String text) {
		Listener[] ls = this.getListeners(SWT.Modify);
		if(ls != null) {
			for(Listener l : ls) {
				this.removeListener(SWT.Modify, l);
			}
		}
		this.setText(text != null ? text : "");
		if(ls != null) {
			for(Listener l : ls) {
				this.addListener(SWT.Modify, l);
			}
		}
		if(model != null && model.getStr("sStatus") != null && !model.getInt("sStatus").equals(2)) {
			//this.setEnabled(false);
			this.setEditable(false);
		}else {
			this.setEditable(true);
		}
			
	}
	public void clear() {
		show("");
	}
	 
	public T getModel() {
		return model;
	}

	public String getField() {
		return field;
	}
	**/
	
	

}
